package com.jgdt.jgdtkasannong_university_gene_backend.service.base.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgdt.jgdtkasannong_university_gene_backend.common.Sysconstant;
import com.jgdt.jgdtkasannong_university_gene_backend.config.enums.SysValueSetEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.dto.EffectiveSequenceDto;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.base.CameraDevice;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.base.CameraDeviceChannel;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.base.CameraDeviceSequence;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.base.CameraDeviceChannelMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.base.CameraDeviceMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.base.CameraDeviceSequenceMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.service.base.CameraDeviceChannelService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.base.CameraDeviceSequenceService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.base.video.FireflyCloudEzvizService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.sys.SysValueSetService;
import com.jgdt.jgdtkasannong_university_gene_backend.utils.RedisUtils;
import com.jgdt.jgdtkasannong_university_gene_backend.vo.base.vido.GetVidoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 摄像头设备序列表 服务实现类
 * </p>
 *
 * @author chenzg
 * @since 2025-05-08
 */
@Service
public class CameraDeviceSequenceServiceImpl extends ServiceImpl<CameraDeviceSequenceMapper, CameraDeviceSequence> implements CameraDeviceSequenceService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private FireflyCloudEzvizService fireflyCloudEzvizService;
    @Autowired
    private CameraDeviceSequenceMapper cameraDeviceSequenceMapper;
    @Autowired
    private CameraDeviceMapper cameraDeviceMapper;
    @Autowired
    private SysValueSetService sysValueSetService;
    @Autowired
    private CameraDeviceChannelService cameraDeviceChannelService;
    @Autowired
    private CameraDeviceChannelMapper cameraDeviceChannelMapper;


    @Override
    public GetVidoVo getGetVidoVo(Map<String,CameraDeviceChannel> cameraDeviceChannelListMap,String userName, Integer sequenceId, Integer channelNo) {
        if(ObjectUtils.isEmpty(cameraDeviceChannelListMap) || StringUtils.isEmpty(userName) || sequenceId==null || channelNo==null){
            log.warn("getGetVidoVo 入参不符合要求...");
            return null;
        }
        GetVidoVo getGetVidoVo=new GetVidoVo();
        String redisKey=Sysconstant.BASE_VIDEO_USER_BROWSING_HISTORY+userName+"-"+sequenceId+"-"+channelNo;
        String channerlMapKey=sequenceId+"-"+channelNo;
        Object result=redisUtils.get(redisKey);
        if(ObjectUtils.isNotEmpty(result)){
             getGetVidoVo = (GetVidoVo)result;
            if (StringUtils.isEmpty(getGetVidoVo.getLiveAddress())) {
                return null;
            } else {
                return getGetVidoVo;
            }
        }else{
            CameraDevice cameraDevice=getCameraDeviceBySequenceId(sequenceId);
            CameraDeviceSequence cameraDeviceSequence = cameraDeviceSequenceMapper.selectById(sequenceId);
            if(ObjectUtils.isEmpty(cameraDeviceSequence)){
                log.warn("getGetVidoVo warn,该序列号不存在！");
                if (StringUtils.isEmpty(getGetVidoVo.getLiveAddress())) {
                    return null;
                } else {
                    return getGetVidoVo;
                }
            }
            //查询最新数据
            GetVidoVo getVidoVo=fireflyCloudEzvizService.getVido(cameraDevice.getCloudAuthAppkey(),cameraDevice.getCloudAuthAppsecret(),channelNo,cameraDeviceSequence.getDvrSerialNumber());
            //通道赋值
            CameraDeviceChannel cameraDeviceChannel=cameraDeviceChannelListMap.get(channerlMapKey);
            if(ObjectUtils.isNotEmpty(cameraDeviceChannel)){
                getVidoVo.setChannelNo(channelNo);
                getVidoVo.setPicUrl(cameraDeviceChannel.getPicUrl());
                getVidoVo.setChannelName(cameraDeviceChannel.getChannelName());
            }
            if(ObjectUtils.isNotEmpty(getVidoVo) && ObjectUtils.isNotEmpty(getVidoVo.getLiveAddress()) ){
                //存入缓存
                String outTime = sysValueSetService.queryPriorityValueByCode(SysValueSetEnums.BASE_VIDEO_USER_BROWSING_HISTORY_OUTTIME.getCode());
                redisUtils.set(redisKey, getVidoVo, Long.valueOf(outTime));
            }
            if (StringUtils.isEmpty(getVidoVo.getLiveAddress())) {
                return null;
            } else {
                return getVidoVo;
            }
        }
    }

    @Override
    public CameraDevice getCameraDeviceBySequenceId(Integer sequenceId) {
        if (sequenceId == null) {
            return null;
        }
        CameraDeviceSequence cameraDeviceSequence = cameraDeviceSequenceMapper.selectById(sequenceId);
        if (ObjectUtils.isEmpty(cameraDeviceSequence) || cameraDeviceSequence.getDeviceId() == null) {
            return null;
        } else {
            return cameraDeviceMapper.selectById(cameraDeviceSequence.getDeviceId());
        }
    }

    @Override
    public List<EffectiveSequenceDto> getEffectiveSequence(CameraDevice cameraDevice) {
        return cameraDeviceMapper.getEffectiveSequence(cameraDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeInsertIds(List<Integer> sequenceIds, List<CameraDeviceChannel> cameraDeviceChannelList) {
        if (CollectionUtil.isEmpty(sequenceIds)) {
            log.warn("completeInsert warn:sequenceId 为空!");
            return;
        }
        LambdaQueryWrapper<CameraDeviceChannel> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(CameraDeviceChannel::getSequenceId,sequenceIds);
        List<CameraDeviceChannel> channelList=cameraDeviceChannelMapper.selectList(queryWrapper);
        if(CollectionUtil.isEmpty(channelList)){
            log.warn("completeInsert warn:channelList 为空!");
        }else{
            List<Integer>  channelListIds=channelList.stream().map(CameraDeviceChannel::getId).collect(Collectors.toList());
            cameraDeviceChannelMapper.deleteBatchIds(channelListIds);
        }
        if(CollectionUtil.isNotEmpty(cameraDeviceChannelList)){
            cameraDeviceChannelService.saveBatchChannelByDeviceList(cameraDeviceChannelList);
        }
    }
}
